package com.leyou.item.web;

import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    private SpecGroupService groupService;
    @Autowired
    private SpecParamService paramService;

//    public SpecController(SpecGroupService groupService, SpecParamService paramService) {
//        this.groupService = groupService;
//        this.paramService = paramService;
//    }

    //GET根据商品分类id，查询规格组的集合   成功实现
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(this.groupService.queryGroupByCategoryId(id));
    }


    //GET根据条件查询规格参数集合    成功实现
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDTO>> queryParamByCondition(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(this.paramService.queryParamByCondition(categoryId, groupId, searching));

    }

    //新增规格组  成功
    @PostMapping("/group")
    public ResponseEntity<Void> addGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.groupService.addGroup(specGroupDTO);
        return ResponseEntity.status(201).build();
    }

    //PUT修改规格组  成功
    @PutMapping("/group")
    public ResponseEntity<Void> upDateGroup(@RequestBody SpecGroupDTO specGroupDTO) {
        this.groupService.upDateGroup(specGroupDTO);
        return ResponseEntity.status(201).build();
    }



    //POST新增规格参数  前端没有显示，但是数据库有内容
    @PostMapping("/param")
    public ResponseEntity<Void> addParam(@RequestBody SpecParamDTO specParamDTO) {
        this.paramService.addParam(specParamDTO);
        return ResponseEntity.status(201).build();
    }

    //POST修改规格参数
    @PutMapping ("/param")
    public ResponseEntity<Void> upDateParam(@RequestBody SpecParamDTO specParamDTO) {
        this.paramService.upDateParam(specParamDTO);
        return ResponseEntity.status(201).build();
    }

    //GET根据分类id查询规格组及组内参数  成功
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDTO>> queryGroupAndParamByCategoryId(@RequestParam("id") Long id){
        return ResponseEntity.ok(this.groupService.queryGroupAndParamByCategoryId(id));
    }

}